<?php

require_once (APPLICATION_PATH . '/../library/phpthumblib/ThumbLib.inc.php');

class PreferenceController extends GTL_Action {

    public function init() {
        parent::init();
        /* Initialize action controller here */
    }

    public function indexAction() {
        $this->_helper->redirector('profile', 'preference', '');
    }

    public function profileAction() {
        $profileForm = new Form_AdminForm();
        $successmessage = "";
        $errorMessageArray = Array();
        $fileName = "";

        //Load Admin Model
        $adminObj = new Model_Admin();
        $data = $adminObj->fetchEntryById($this->USER->adm_id);
        $this->view->form = $profileForm->populate($data);

        if ($this->getRequest()->isPost()) {
            $postedData = $this->_request->getPost();
            if (!$profileForm->isValid($postedData)) {
                $errorMessage = $profileForm->getMessages();
                foreach ($errorMessage as $_err) {
                    foreach ($_err as $_val) {
                        $errorMessageArray[] = $_val;
                    }
                }
            } else {
                if ($postedData['old_password'] != "") {
                    if ($postedData['adm_password'] == "") {
                        $errorMessageArray[] = "Password should not be blank.";
                    }
                    if ($postedData['adm_confirmpassword'] == "") {
                        $errorMessageArray[] = "Confirm Password should not be blank.";
                    }

                    if ($postedData['adm_password'] != "" && $postedData['adm_confirmpassword'] != "") {
                        if ($postedData['adm_password'] != $postedData['adm_confirmpassword']) {
                            $errorMessageArray[] = "Password and Confirm Password should not be blank.";
                        }
                    }
                }
                
                //Image Validation
                if ($_FILES['adm_logo']['name'] != '') {
                    $gtlObj = new GTL_Common();
                    $imageAllowedFormat = $gtlObj->allowedImageFormats();
                    $responseText = $gtlObj->image_validate($imageAllowedFormat, 'adm_logo');
                    if ($responseText == 'IMPROPER_FORMAT') {
                        $errorMessageArray[] = 'Please upload image with valid file format.';
                    }
                }
                
                if (count($errorMessageArray) > 0) {
                    
                } else {
                    $fileName = $data['adm_logo'];

                    $upload = new Zend_File_Transfer_Adapter_Http();
                    foreach ($upload->getFileInfo() as $fields => $asFileInfo) {
                        if ($asFileInfo['name'] != '') {
                            $folder = $this->config->UPLOAD_PATH . "company/";
                            $upload->setDestination($folder);
                            $originalFilename = pathinfo($asFileInfo['name']);

                            $fileName = preg_replace("/[^-_a-zA-Z0-9]+/i", "_", $originalFilename['filename']) . '_' . time() . '.' . $originalFilename['extension'];
                            $upload->addFilter('Rename', $fileName, 'filename');
                            $upload->receive($fields);

                            $thumb2 = PhpThumbFactory::create($folder . $fileName);
                            $thumb2->resize(200, 200);
                            $thumb2->save($folder . '/' . 'thumb_' . $fileName, 'jpg');
                        }
                        $data['adm_logo'] = $fileName;
                    }

                    $profileData['adm_id'] = $this->USER->usr_id;
                    $profileData['adm_firstname'] = $postedData['adm_firstname'];
                    $profileData['adm_lastname'] = $postedData['adm_lastname'];
                    $profileData['adm_companyname'] = $postedData['adm_companyname'];
                    $profileData['adm_registration_number'] = $postedData['adm_registration_number'];
                    $profileData['adm_logo'] = $fileName;
                    $profileData['adm_address'] = $postedData['adm_address'];
                    $profileData['adm_telephone'] = $postedData['adm_telephone'];
                    $profileData['adm_fax'] = $postedData['adm_fax'];
                    $profileData['adm_mobile'] = $postedData['adm_mobile'];
                    $adminObj->save($profileData, 'adm_id');
                    $successmessage = "Profile updated successfully";
                    if ($postedData['adm_password'] != '') {
                        if (md5($postedData['old_password']) != $this->USER->lgn_password) {
                            $errorMessageArray[] = "Please enter correct old password!";
                            $successmessage = "";
                        } else {
                            $loginModel = new Model_Login();
                            $dataUpdate = array();
                            $authData = Zend_Auth::getInstance()->getIdentity();
                            $authData->lgn_password = md5($postedData['adm_password']);
                            $dataUpdate['lgn_password'] = md5($postedData['adm_password']);
                            $dataUpdate['lgn_id'] = $this->USER->lgn_id;
                            $loginModel->save($dataUpdate, 'lgn_id');
                            $authData = Zend_Auth::getInstance()->getIdentity();
                            $authData->lgn_password = md5($postedData['adm_password']);
                        }
                    }
                }
            }
        }

        $this->view->data = $data;
        $this->view->errorMessageArray = $errorMessageArray;
        $this->view->succesMessage = $successmessage;
        $this->view->form = $profileForm;
        $indexUrl = $this->view->url(array('controller' => 'index', 'action' => 'index'), null, TRUE);
        $profileUrl = $this->view->url(array('controller' => 'preference', 'action' => 'profile'), null, TRUE);
        $this->view->breadcrumbs = array($indexUrl => "Home", $profileUrl => "Site Preferences", "" => "Edit profile");
    }
    
    
    public function deletecompanylogoAction() {
        $adminModel = new Model_Admin();
        $data = $adminModel->fetchEntryById($this->USER->adm_id);
        $logo = $data['adm_logo'];
        $folder = $this->config->UPLOAD_PATH . "company/";
        $logo = $folder . $data['adm_logo'];
        $thumb_logo = $folder . "thumb_" . $data['adm_logo'];
        if (file_exists($logo)) {
            @unlink($logo);
        }
        if (file_exists($thumb_logo)) {
            @unlink($thumb_logo);
        }

        $profileData = Array();
        $profileData['adm_id'] = $this->USER->usr_id;
        $profileData['adm_logo'] = '';
        $adminModel->save($profileData, 'adm_id');

        exit;
    }

    public function uomAction() {
        
        $successmessage = "";
        $errorMessageArray = Array();
        $uom = "";
        $uomArray = Array();
        $uomModel = new Model_Uom();


        if ($this->getRequest()->isPost()) {
            $postedData = $this->_request->getPost();
            $uom = $postedData['uom'];
            if (trim($uom) != '') {
                $gtlCommonObj = new GTL_Common();
                $key = $gtlCommonObj->generate_seo_link($uom);
                $value = $uom;
                $uomDataArray = Array();
                $uomDataArray['uom_key'] = $key;
                $uomDataArray['uom_value'] = $value;
                $uomModel->save($uomDataArray, 'uom_id');
                $successmessage = "Unit of measurement added successfully";
                $uom = "";
            } else {
                $errorMessageArray[] = "Please enter unit of measurement";
            }
        }
        $uomArray = $uomModel->fetchUom();
        $this->view->uom = $uom;
        $this->view->uomArray = $uomArray;
        $this->view->errorMessageArray = $errorMessageArray;
        $this->view->succesMessage = $successmessage;
    }
    
    public function deleteuomAction() {
        $uomModel = new Model_Uom();
        if ($this->getRequest()->isPost()) {    
            $postedData = $this->_request->getPost();
            $uom_id = $postedData['uom_id'];
            $delete = $uomModel->deleteUom($uom_id);
            echo $delete; exit;
        }
        exit;
    }

}
?>
